<?php
require_once APP_PATH . '/category/model.php';
function category_create_action() {
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		//提交保存
		category_save_action();
	} else {
		//显示编辑页面
		category_show_action();
	}

}
/**
 * 显示category页面
 * @param  string
 * @return [type]
 */
function category_show_action() {
	checkLogin();
	$hash = isset($_REQUEST['hash']) ? trim($_REQUEST['hash']) : '';

	if (empty($hash)) {
		include APP_ADMIN_VIEW . 'category/create.php';
	} else {
		$hash = filter_data($hash);
		//获取数据
		$category_data = category_model_get_by_hash($hash);
		//输出
		if ($category_data) {
			include APP_ADMIN_VIEW . 'category/create.php';
		} else {
			show_not_found();
		}
	}

}

function category_save_action() {
	checkLogin();
	$hash = isset($_REQUEST['hash']) ? trim($_REQUEST['hash']) : '';
	if (empty($hash)) {
		//新建
		$category_name = isset($_POST['category_name']) ? trim($_POST['category_name']) : '';
		$category_alias = isset($_POST['category_alias']) ? trim($_POST['category_alias']) : '';
		$parent_id = isset($_POST['parent_id']) ? intval($_POST['parent_id']) : 0;

		$keywords = isset($_POST['keywords']) ? trim($_POST['keywords']) : '';
		$description = isset($_POST['description']) ? trim($_POST['description']) : '';
		$status = isset($_POST['status']) ? intval(trim($_POST['status'])) : 0;

		//获取数据
		$ret = category_model_insert($category_name, $parent_id, $category_alias, $status, $keywords, $description);
		if ($ret) {
			header("Location: /" . APP_ADMIN_URI . "/category/list");
		}
	} else {
		//更新
		//新建
		$category_name = isset($_POST['category_name']) ? trim($_POST['category_name']) : '';
		$category_alias = isset($_POST['category_alias']) ? trim($_POST['category_alias']) : '';

		$parent_id = isset($_POST['parent_id']) ? intval($_POST['parent_id']) : 0;

		$keywords = isset($_POST['keywords']) ? trim($_POST['keywords']) : '';
		$description = isset($_POST['description']) ? trim($_POST['description']) : '';
		$status = isset($_POST['status']) ? intval($_POST['status']) : 0;

		//获取数据
		$ret = category_model_update_by_hash($hash, $category_name, $parent_id, $category_alias, $status, $keywords, $description);
		if ($ret) {
			header("Location: /" . APP_ADMIN_URI . "/category/list");
		}
	}

}